Xbasic

Extension::JsonExtractMapped Method

Syntax

.ExtractMapped as c (jsonMap as C, json as C)

Arguments

jsonMapCharacter

Template that is the shape of JSON to extract from.

jsonCharacter

Structured data for populate from

Description

Extract mapped json (create the flat json with the element).

Discussion

Creates a flattened JSON object from a complex JSON object. The flattened JSON object has all properties at the top level, with no nested objects are arrays. The extension::JSON::ExtractMapped() method allows you to do this. Assume you have a complex JSON document as shown below. Note that the only top-level property is "person"

dim jsonIn as c
jsonIn = <<%json%
{
    "person": {
        "id": "001",
        "firstname": "John",
        "lastname": "Dublic",
        "address": [
            "12 Main Street",
            "Box 20"
        ],
        "otherInfo" : {

            "x": "value of x",

            "y" : "value of y"

        }
    }
}
%json%

Say you want to flatten this JSON document so that all properties are at the top level and also change some of the property names. The resulting JSON document should look like this:

{
    "id": "001",
    "fname": "john",
    "lname": "public",
    "address1": "12 Main Street",
    "address2": "box 20",
    "x_value": "value of x",
    "this is the y value": "value of y"
}

Here is the Map that would define to achieve the above transformation:

dim jsonMap as c
jsonMap = <<%json%
{
    "person" : {
        "id" : "id" ,
        "firstname" : "fname" ,
        "lastname" : "lname" ,
        "address" : [ "address1" , "address2" ],
        "otherInfo" : {

            "x" : "x_value",

            "y" : "this is the y value"

        }
    }
}
%json%

Here is how you would transform the input JSON (jsonIn) using the map (jsonMap)

dim jsonout as c
jsonOut = extension::Json::ExtractMapped(jsonMap,jsonIn)
dim jsonout2 as c
jsonout2 = json_reformat(jsonout) 'format the JSON